// pages/index/demand.js
var i = getApp(), e = i.requirejs("core");
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		picList: [],
		pic_select_num: 5,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	bindKeyInput(e) {
		this.setData({
			content: e.detail.value
		})
	},
	onLoad: function (options) {
		var t = this;
		e.get("shop/focus", {}, function (res) {
			t.setData({
				scanimg: res.focus
			})
		});
	},
	editPicture(e) {
		const index = e.currentTarget.dataset.index
		if (index < 0) return
		if (index == 'empty') {
			wx.showActionSheet({
				itemList: ['图片'],
				success: res => {
					if (res.tapIndex === 0) this.choosePicture(index)
				}
			})
		} else {
			wx.showActionSheet({
				itemList: ['删除'],
				success: res => {
					if (res.tapIndex === 0) this.removePicture(index)
				}
			})
		}
	},
	removePicture(index) {
		const picList = this.data.picList.slice()
		picList.splice(index, 1)
		var newpic = this.data.pic_select_num
		this.setData({
			picList,
			pic_select_num: (newpic + 1)
		})
		console.log(this.data.picList);
		// this.delPic()
	},
	// delPic() {
	// 	var that = this
	// 	wx.request({
	// 		url: api.pt.delPic,
	// 		data: {
	// 			id: this.data.id,
	// 			picList: that.data.picList,
	// 			access_token: this.data.access_token,
	// 		},
	// 		success: res => {
	// 			wx.hideLoading()
	// 		},
	// 		fail: err => {
	// 			wx.hideLoading()
	// 			console.log(err)
	// 		}
	// 	})
	// },
	choosePicture(index) {
		wx.chooseImage({
			count: this.data.pic_select_num,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success: res => {
				const tempFilePaths = res.tempFilePaths
				wx.showLoading({
					title: '图片上传中',
					mask: true,
				})
				var successUp = 0; //成功个数
				var failUp = 0; //失败个数
				var length = res.tempFilePaths.length; //总共个数
				var i = 0; //第几个
				this.uploadDIY(tempFilePaths, successUp, failUp, i, length)
			}
		})
	},
	uploadDIY(filePaths, successUp, failUp, i, length) {
		wx.uploadFile({
			url: "https://newproject.gzchujiao.com/app/ewei_shopv2_api.php?i=1&r=util.uploader.upload&file=file",
			filePath: filePaths[i],
			name: 'file',
			formData: {

			},
			success: (res) => {
				successUp++;
				let {
					data
				} = res
				data = JSON.parse(data)
				if (data.error === 0) {
					var picList = this.data.picList
					console.log(data.files[0].url)
					picList.push({
						empty: false,
						picUrl: data.files[0].url
					})
					i++;
					var newpic = this.data.pic_select_num
					this.setData({
						picList,
						pic_select_num: (newpic - 1)
					})
				}
			},
			fail: (res) => {
				failUp++;
			},
			complete: () => {

				if (i == length) {
					wx.hideLoading()
					wx.showToast({
						title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败！',
					})


				} else { //递归调用uploadDIY函数
					this.uploadDIY(filePaths, successUp, failUp, i, length);
				}
			},
		});
	},
	sunbitli:function(){
		var piclist = [];
		for (var i of this.data.picList){
			piclist.push(i.picUrl)
		}
		var content = this.data.content;
		if (content == '' || typeof (content) == 'undefined'){
			wx.showToast({
				title: '请填写需求',
				icon:'none'
			})
			return
		}
		e.get("shop/demand", { demand:content,thumb_url:piclist}, function (res) {
			if (res.error == 0){
				wx.showToast({
					title: '提交成功',
					icon:'none'
				})
				setTimeout(function(){
					wx.navigateBack({
						
					})
				},800)
			}else{
				wx.showToast({
					title: '保存失败，请重新保存',
					icon: 'none'
				})
			}
		});	
	},
	// liupload: function () {
	// 	var e = this;
	// 	wx.chooseImage({
	// 		success: function (n) {
	// 			wx.showLoading({
	// 				title: '上传中',
	// 			})	
	// 			var v = o.getUrl("util/uploader/upload", {
	// 				file: "file"
	// 			}), i = n.tempFilePaths;
	// 			wx.uploadFile({
	// 				url: v,
	// 				filePath: i[0],
	// 				name: "file",
	// 				success: function (n) {
	// 					wx.hideLoading();
	// 					var o = JSON.parse(n.data);
	// 					console.log(o.files);
	// 						e.setData({
	// 							piclist: o.files
	// 						}) 
	// 				}
	// 			});
	// 		}
	// 	});
	// },
	// editPicture:function(e) {
	// 	var that = this;
	// 		wx.showActionSheet({
	// 			itemList: ['图片'],
	// 			success: res => {
	// 				if (res.tapIndex === 0) that.liupload();
	// 			}
	// 		})
	// },	
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})